package com.yuju.scheduler.service.task;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.yuju.enums.StatusEnum;
import com.yuju.hotel.model.RoomModel;
import com.yuju.hotel.model.RoomRepairModel;
import com.yuju.hotel.service.IRoomRepairService;
import com.yuju.hotel.service.IRoomService;
import com.yuju.order.enums.RoomOrderStatusEnum;
import com.yuju.order.model.RoomOrderModel;
import com.yuju.order.service.IRoomOrderService;
import com.yuju.util.DateUtil;
import com.yuju.util.Utils;

@Component
public class NightAudit {

	@Autowired
	private IRoomOrderService roomOrderService;
	@Autowired
	private IRoomService roomService;
	@Autowired
	private IRoomRepairService roomRepairService;
	
	
	
	/**
	 * 
	 * 功能：预离未离的自动延住一天:定时每天0：01分，sql有日期比较<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月18日 下午4:00:37 <br/>
	 */
	@Scheduled(cron="0 1 0 * * ?")
	 public void updateCheckoutDate(){
		roomOrderService.updateCheckoutDate();
	 }
	
	/**
	 * 
	 * 功能：将在住房变为脏房<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月18日 下午4:44:43 <br/>
	 */
	@Scheduled(cron="0 1 0 * * ?")
	 public void updateRoomDirtyStatus(){
		//查询所有入住订单
		RoomOrderModel roomOrderModel = new RoomOrderModel();
		roomOrderModel.setCheckinStatus(RoomOrderStatusEnum.CHECKIN.getValue());
		List<RoomOrderModel> list = roomOrderService.list(roomOrderModel);
		//根据房间号更新房间脏净状态
		for(RoomOrderModel model : list){
			RoomModel roomModel = new RoomModel();
			roomModel.setId(model.getRoomId());
			roomModel.setIsDirty(StatusEnum.TRUE.getValue());
			roomService.updateSelective(roomModel);
		}
	 }
	
	/**
	 * 
	 * 功能：未解除的维修房，释放维修房//定时任务23：59执行，sql有日期比较<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月18日 下午4:54:24 <br/>
	 */
	@Scheduled(cron="00 59 23 * * ?")
	 public void updateRepairStatus(){
		//根据日期和状态 查询该日期是否有 未解除的维修信息
		RoomRepairModel model = new RoomRepairModel();
		model.setRepairStatus(StatusEnum.TRUE.getValue());
		model.setEndDay(DateUtil.parseDate(new Date(), "yyyy-MM-dd"));
		List<RoomRepairModel> list = roomRepairService.list(model);
		
		if(Utils.isNotNull(list)){
			//查询是否需要将对应房间设置为脏房
			for (int i = 0; i < list.size(); i++) {
				//1 是设置为脏房
				if(list.get(0).getIsSetDirty() == 1){
					RoomModel roomModel = new RoomModel();
					roomModel.setId(list.get(i).getRoomId());
					roomModel.setIsDirty(StatusEnum.TRUE.getValue());
					roomService.updateSelective(roomModel);
				}
				RoomRepairModel roomRepairModel = new RoomRepairModel();
				//更新维修状态
				roomRepairModel.setRepairStatus(StatusEnum.FALSE.getValue());
				//更新结束时间
				roomRepairModel.setEndDay(new Date());
				roomRepairModel.setGmtModified(new Date());
				roomRepairService.updateSelective(roomRepairModel);
			}
		}
	}
	
	
	
}
